package com.liuwei.msmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.liuwei.msmall.common.utils.PageUtils;
import com.liuwei.msmall.common.utils.Query;
import com.liuwei.msmall.product.dao.AttrGroupDao;
import com.liuwei.msmall.product.entity.AttrAttrgroupRelationEntity;
import com.liuwei.msmall.product.entity.AttrGroupEntity;
import com.liuwei.msmall.product.service.AttrAttrgroupRelationService;
import com.liuwei.msmall.product.service.AttrGroupService;
import com.liuwei.msmall.product.service.AttrService;
import com.liuwei.msmall.product.service.CategoryService;
import com.liuwei.msmall.product.vo.AttrVo;
import com.liuwei.msmall.product.vo.SpuItemAttrGroupVo;
import com.liuwei.msmall.product.vo.resp.AttrGroupWithAttrRespVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    CategoryService categoryService;

    @Resource
    AttrService attrService;

    @Resource
    AttrAttrgroupRelationService attrgroupRelationService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String key = (String) params.get("key");

        LambdaQueryWrapper<AttrGroupEntity> queryWrapper = new LambdaQueryWrapper<AttrGroupEntity>();
        if (StringUtils.isNotBlank(key)){
            queryWrapper.like(AttrGroupEntity::getAttrGroupName,key).or().eq(AttrGroupEntity::getCatelogId,key);
        }

        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Long categoryId, Map<String, Object> params) {

        LambdaQueryWrapper<AttrGroupEntity> queryWrapper = new LambdaQueryWrapper<>();
        //模糊查询
        String key =(String) params.get("key");
        if (StringUtils.isNotBlank(key)){
            queryWrapper.like(AttrGroupEntity::getAttrGroupName,key).or()
                    .eq(AttrGroupEntity::getAttrGroupId,key).or()
                    .eq(AttrGroupEntity::getCatelogId,key);

        }


        if (categoryId.longValue() !=0){
            queryWrapper.eq(AttrGroupEntity::getCatelogId,categoryId);
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                queryWrapper
        );
        //查出分类名
        if(!CollectionUtils.isEmpty(page.getRecords())){

            List<Long> catIds = page.getRecords().stream().map(i -> i.getCatelogId()).collect(Collectors.toList());

            Map<Long,String> cateMaps =  categoryService.getCateMap(catIds);

            if (!CollectionUtils.isEmpty(cateMaps)){
                page.getRecords().stream().forEach(group->{
                 group.setCategoryName(cateMaps.get(group.getCatelogId()));
                });
            }
        }
        return new PageUtils(page);
    }

    @Override
    public AttrGroupEntity getOneWithCatePath(Long attrGroupId) {

        //拿到分类id
        AttrGroupEntity attrGroupEntity = this.getById(attrGroupId);
        Long catelogId = attrGroupEntity.getCatelogId();
        //查询父分类id路径
        Long[] catePath = categoryService.getCatePathById(catelogId);
        attrGroupEntity.setCatePath(catePath);

        return attrGroupEntity;
    }

    @Override
    public  List<AttrVo>  getAttrRelationList(Long attrgroupId) {

        List<Long> attrIds = getAttrIdsByGroupId(attrgroupId);

        List<AttrVo> vos =  attrService.getAttrInfoByIds(attrIds);

        return vos;
    }

    private List<Long> getAttrIdsByGroupId(Long attrgroupId) {

        List<AttrAttrgroupRelationEntity> relationEntities = attrgroupRelationService.list(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>()
                .eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrgroupId));
        if (CollectionUtils.isEmpty(relationEntities)){
            return Lists.newArrayList();
        }
        //拿到属性值id
        List<Long> attrIds = relationEntities.stream().map(relation -> relation.getAttrId()).collect(Collectors.toList());
        return attrIds;
    }

    @Override
    public PageUtils  getNoAttrRelationList(Long attrgroupId,Map<String,Object> params) {

        List<Long> attrIds = new ArrayList<>();
        //拿到关联表中所有关联的属性id
        List<AttrAttrgroupRelationEntity> list = attrgroupRelationService.list();
        if (!CollectionUtils.isEmpty(list)){
            attrIds = list.stream().map(i -> i.getAttrId()).distinct().collect(Collectors.toList());
        }

        PageUtils page =attrService.getNoAttrRelationPage(attrIds,params);
        return page;
    }

    @Override
    public List<AttrGroupWithAttrRespVo> getAttrGroupAndAttrByCateId(Long cateId) {

        List<AttrGroupWithAttrRespVo> respVos = new ArrayList<>();

        //查出分组
        List<AttrGroupEntity> attrGroupEntities = this.list(new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, cateId));

        if (CollectionUtils.isEmpty(attrGroupEntities)){
            return respVos;
        }

        attrGroupEntities.stream().forEach(attrGroupEntity -> {
            AttrGroupWithAttrRespVo respVo = new AttrGroupWithAttrRespVo();
            BeanUtils.copyProperties(attrGroupEntity,respVo);
            respVo.setAttrs(attrService.getAttrListByGroupId(attrGroupEntity.getAttrGroupId()));
            respVos.add(respVo);
        });

        return respVos;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrBySpuId(Long spuId, Long catalogId) {

        List<SpuItemAttrGroupVo> spuItemAttrGroupVos= this.getBaseMapper().getAttrGroupWithAttrBySpuId(spuId,catalogId);

        return spuItemAttrGroupVos;

    }

}
